package com.eh.httpClient;

import java.util.Comparator;
import java.util.Map;
import java.util.TreeMap;

/**
 * Created by David Li on 2016/4/19.
 */
public class HttpClientUtil {
    public static String buildSignData(Map<String, Object> map) {
        Map<String, Object> requestMap = transfer2Sorted(map);
        return buildSignString(requestMap);
    }

    private static Map<String, Object> transfer2Sorted(Map<String, Object> map) {
        Map<String, Object> m = new TreeMap<>(new Comparator<String>() {
            public int compare(String o1, String o2) {
                // 升序排序
                return o1.compareTo(o2);
            }
        });
        m.putAll(map);
        return m;
    }

    private static String buildSignString(Map<String, Object> map) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> e : map.entrySet()) {
            sb.append(e.getKey()).append("=").append(e.getValue()).append("&");
        }
        sb.deleteCharAt(sb.length() - 1);
        return sb.toString();
    }
}
